package model;

import java.util.ArrayList;
public class Order
{
    private static long idCounter = 0;
    
    
    private String id;
    private ArrayList<OrderLine> orderLineList;
    private Customer customer;
    private double total;
    private String status;
    
    public Order(Customer customer) {
      this.customer = customer;
      orderLineList = new ArrayList<OrderLine>();
      total = 0;
      id = createID();
    }
     
    
    // SET - GET
    
    public void setID(String id)
    {
        this.id = id;
    }
    
    public String getID() 
    {
        return id;
    }
    
    public boolean addOrderLine(Product product, int productQuantity)
    {
        if(product.getAmount()> productQuantity)
        {
            OrderLine ol = new OrderLine(product, productQuantity);
            orderLineList.add(ol); 
            total += product.getPrice()*productQuantity;
            return true;
        }
        else return false;
    }
    public Product getProduct(String productName)//* maybe id or something for finding specific product 
    {
        for(OrderLine ol : orderLineList)
        {
            if(ol.getProduct().getProductName().equals(productName))
            {
                return ol.getProduct();
            }
        }
        return null;
    } 
   
    public void calculateTotal() {
        for(OrderLine ol : orderLineList)
        {
            total += ol.getProduct().getPrice() * ol.getProductQuantity();
        }
        total = total - (total * customer.getGroup().getAmountDiscount());
    }
    
    public void setCustomer(Customer customer)
    {
        this.customer = customer;
    }
    public Customer getCustomer() 
    {
        return customer;
    }
    
    public void setTotal(double total) 
    {
        this.total = total;
    }
    public double getTotal() 
    {
        calculateTotal();
        return total;
    }
    
    public void setStatus(String status)
    {
        this.status = status;
    }
    public String getStatus()
    {
        return status;
    }

    
    public String showPaymentType() {
        return customer.getGroup().getPaymentType();
    }
    
    
    
    public ArrayList<Product> getAllProducts() { // Name needs to be changed, unless we are lazy
        ArrayList<Product> list = new ArrayList<Product>();
        for(OrderLine ol : orderLineList)
        {
            list.add(ol.getProduct());
        }
        return list;
    }
    
    public static synchronized String createID()
    {
        return String.valueOf(idCounter++);
    }
}
